package com.itheima.health.service.impl;

import com.itheima.health.constant.MessageConstant;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Member;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Map;

/**
 * @author deng
 * @package com.itheima.health.service.impl
 * @date Created in 2024/7/14 16:40
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private SetmealDao setmealDao;

    @Override
    public Result order(Map<String, String> map) {
        String telephone = map.get("telephone");
        int setmealId = Integer.parseInt(map.get("setmealId"));
        String orderDateStr = map.get("orderDate");
        Date orderDate = null;
        try {
            orderDate = DateUtils.parseString2Date(orderDateStr);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DATE_FORMAT_ERROR);
        }
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(orderDate);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        Member member = memberDao.findByTelephone(telephone);
        if (member == null) {
            member = new Member();
            member.setName(map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard(map.get("idCard"));
            member.setSex(map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.updateReservationsByOrderDate(orderSetting);
        Order order = new Order(member.getId(), orderDate, map.get("orderType"), Order.ORDERSTATUS_NO, setmealId);
        orderDao.add(order);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    @Override
    public Map findById(Integer id) {
        Map map = orderDao.findById(id);
        Integer memberId = Integer.valueOf(map.get("member_id").toString());
        String memberName = memberDao.findNameById(memberId);
        map.put("member",memberName);
        Integer setmealId = Integer.valueOf(map.get("setmeal_id").toString());
        String name = setmealDao.findNameById(setmealId);
        map.put("setmeal", name);
        return map;
    }
}

